package com.jdk.learn.thread;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * ReentrantLock实现版本
 */
public class LockSynchronizeDemo3 implements Runnable{
    public Lock lock=new ReentrantLock();
    @Override
    public void run() {
        get();
    }

    public  void get(){
        lock.lock();
        try {
            Thread.sleep(30);
            System.out.println("name:"+Thread.currentThread().getName()+",get");
            set();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
//            lock.unlock();
        }
    }
    public  void set(){
        try {
            Thread.sleep(30);
            System.out.println("name:"+Thread.currentThread().getName()+",set");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }

    public static void main(String[] args) {
        LockSynchronizeDemo3 lockSynchronizeDemo=new LockSynchronizeDemo3();
        new Thread(lockSynchronizeDemo,"1").start();
        new Thread(lockSynchronizeDemo,"2").start();
        new Thread(lockSynchronizeDemo,"3").start();
        new Thread(lockSynchronizeDemo,"4").start();
    }
}
